//
//  SegmentViewController.m
//  SafeFoodManagerDemo
//
//  Created by bob on 2017/10/17.
//  Copyright © 2017年 bob. All rights reserved.
//

#import "SegmentViewController.h"

@interface SegmentViewController ()<UIScrollViewDelegate>

@property(nonatomic,strong)UISegmentedControl *segmentedControl;

@end

@implementation SegmentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self addCenterView:self.segmentedControl];
    
    [self.view addSubview:self.backScrollView];
}


-(UIScrollView*)backScrollView
{
    if (!_backScrollView) {
     
        _backScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
        _backScrollView.delegate=self;
        
        _backScrollView.pagingEnabled=YES;
        
        _backScrollView.showsVerticalScrollIndicator=NO;
        
        _backScrollView.showsHorizontalScrollIndicator=NO;
        
         _backScrollView.contentSize=CGSizeMake(SCREEN_WIDTH*2, 0);
        
    }
    return _backScrollView;
}


-(UISegmentedControl*)segmentedControl
{
    if (!_segmentedControl) {
 
        _segmentedControl = [[UISegmentedControl alloc]initWithItems:self.segmentedArray];
        
        _segmentedControl.frame=CGRectMake(0, 0, 150*SCREEN_WIDTH/320, 30);
        
        _segmentedControl.selectedSegmentIndex = 0;
        
        _segmentedControl.tintColor = [UIColor whiteColor];
        
        [_segmentedControl addTarget:self action:@selector(didClicksegmentedControlAction:)forControlEvents:UIControlEventValueChanged];
    }
    
    return _segmentedControl;
}


-(void)didClicksegmentedControlAction:(UISegmentedControl*)seg
{
    NSInteger Index = seg.selectedSegmentIndex;
    
        if (Index==0) {
    
            [self.backScrollView setContentOffset:CGPointMake(0, 0) animated:NO];
            
        }else{
    
    
            [self.backScrollView setContentOffset:CGPointMake(SCREEN_WIDTH, 0) animated:NO];
        }
    
        KPostNotification(@"SegMentLoad", nil);//发生通知
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGPoint offpoint=scrollView.contentOffset;
    if (offpoint.x<SCREEN_WIDTH) {
        self.segmentedControl.selectedSegmentIndex = 0;
    }else{
        self.segmentedControl.selectedSegmentIndex = 1;
    }
    //通知加载数据
    KPostNotification(@"SegMentLoad", nil);//发生通知
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
